package com.tengju.bff.interfaces.login;


import com.tengju.bff.interfaces.login.assembler.SmsCodeAssembler;
import com.tengju.bff.interfaces.login.vo.ManMachineValidateRegisterVO;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.servlet.NotTokenValid;
import com.tengju.user.application.login.SmsVerifyCodeService;
import com.tengju.user.application.login.dto.CheckSmsCodeDTO;
import com.tengju.user.application.login.dto.SendSmsCodeDTO;
import com.tengju.user.application.login.dto.SendSmsCodeV2DTO;
import com.tengju.user.application.login.vo.CheckSmsCodeVO;
import com.tengju.user.application.shared.ApplicationException;
import com.tengju.user.domain.model.sms.CheckSmsCodeData;
import com.tengju.user.domain.model.sms.SendSmsVerifyCodeData;
import com.tengju.user.domain.shared.DomainException;
import com.tengju.user.domain.shared.ResultCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author wuziyang
 */
@Validated
@RestController
@Api(tags = "短信验证码服务")
@RequestMapping(value = "login/sms/")
@Slf4j
public class SmsCodeController {

    @Autowired
    SmsVerifyCodeService smsVerifyCodeService;

    @ApiOperation(value = "极验初始化", notes = "极验初始化")
    @RequestMapping(value = "geetest/register", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @NotTokenValid
    public ApiResponse<ManMachineValidateRegisterVO> geetestRegister() {

        try {
            return ApiResponse.newSuccess(SmsCodeAssembler.convertToVo(smsVerifyCodeService.geetestRegister()));
        } catch (DomainException | ApplicationException e) {
            log.error("极验初始化失败", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }

    }

    
    @ApiOperation(value = "发送短信验证码", notes = "发送短信验证码")
    @RequestMapping(value = "getVerificationCode", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @NotTokenValid
    public ApiResponse<Boolean> sendSmsVerifyCode(SendSmsCodeDTO sendSmsCodeDTO) {

        try {
            SendSmsVerifyCodeData sendSmsVerifyCodeData = SmsCodeAssembler.dtoToModel(sendSmsCodeDTO);
            return ApiResponse.newSuccess(smsVerifyCodeService.sendSmsVerifyCode(sendSmsVerifyCodeData,false));
        } catch (DomainException | ApplicationException e) {
            log.warn("手机号:{},短信验证码发送失败：",sendSmsCodeDTO.getMobile(), e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }

    }

    @ApiOperation(value = "发送短信验证码", notes = "发送短信验证码")
    @RequestMapping(value = "v2/getVerificationCode", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @NotTokenValid
    public ApiResponse<Boolean> sendSmsVerifyCodeV2(SendSmsCodeV2DTO sendSmsCodeV2DTO) {

        try {
            SendSmsVerifyCodeData sendSmsVerifyCodeData = SmsCodeAssembler.dtoToModel(sendSmsCodeV2DTO);
            return ApiResponse.newSuccess(smsVerifyCodeService.sendSmsVerifyCode(sendSmsVerifyCodeData,true));
        } catch (DomainException | ApplicationException e) {
            log.warn("手机号:{},短信验证码发送失败：",sendSmsCodeV2DTO.getMobile(), e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }

    }

    @ApiOperation(value = "校验短信验证码", notes = "校验短信验证码")
    @RequestMapping(value = "checkVerificationCode", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @NotTokenValid
    public ApiResponse<CheckSmsCodeVO> checkSmsCode(@Valid @RequestBody CheckSmsCodeDTO checkSmsCodeDTO) {

        try {
            CheckSmsCodeData checkSmsCodeData = SmsCodeAssembler.dtoToModel(checkSmsCodeDTO);
            return ApiResponse.newSuccess(smsVerifyCodeService.checkSmsCode(checkSmsCodeData));
        } catch (DomainException | ApplicationException e) {
            log.warn("手机号:{},短信验证码校验失败：",checkSmsCodeDTO.getMobile(), e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }

    }
}
